package com.qianyitian.hope2.analyzer.controller;

import com.qianyitian.hope2.analyzer.model.KLineInfo;
import com.qianyitian.hope2.analyzer.service.StockService;
import com.qianyitian.hope2.analyzer.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
public class IndexController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    String[] codeArray = new String[]{"sh000001", "sz399001", "sh000300", "sz399006"};
    double[] lowArray = new double[]{2562, 7936, 3301, 1656};


    @Autowired
    private StockService stockService;

    public IndexController() {
    }

    private Map getMap() {
        Map map = new HashMap();
        map.put("generateTime", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        return map;
    }

    @RequestMapping("/index-risk-tip")
    @CrossOrigin
    public Map risk() {
        Map map = getMap();
        List list = new LinkedList();
        map.put("data", list);
        for (int i = 0; i < codeArray.length; i++) {
            Map m = getTip(codeArray[i], lowArray[i]);
            list.add(m);
        }

        return map;
    }

    private Map getTip(String code, double low) {
        List<KLineInfo> kLineInfos = stockService.getStockDaily(code).getkLineInfos();
        double close = kLineInfos.get(kLineInfos.size() - 1).getClose();
        double range = Utils.calcRange(close, low);
        String lable = Utils.double2Percentage(range * -1);
        Map map = new HashMap();
        map.put(code + "-now", close);
        map.put(code + "-low", low);
        map.put(code + "-drop", lable);
        return map;
    }

}
